<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 找到数组中出现最频繁的偶数 -->
    <script>
      var mostFrequentEven = function (nums) {
        let arr = nums.filter(item => item % 2 === 0)
        if (arr.length === 0) {
          return -1
        } else {
          let mymap = new Map()
          for (let k of arr) {
            mymap.set(k, (mymap.get(k) || 0) + 1)
          }
          // console.log(mymap)
          let vals = Array.from(mymap.values())
          vals.sort((a, b) => b - a)
          // 获取最多的出现次数maxnum
          let maxnum = vals[0]

          let resarr = []
          for (let [key, value] of mymap) {
            if (value === maxnum) {
              resarr.push(key)
            }
          }
          resarr.sort((a, b) => a - b)
          return resarr[0]
        }
      }
      let res = mostFrequentEven([0, 1, 2, 2, 4, 4, 1])
      console.log(res)
    </script>
  </body>
</html>
